package com.sgb.kjwl.view.widget.transport;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ChooseFunctionPopwindowLayoutBinding;
import com.sgb.kjwl.model.entity.self.TypeChooseEntity;
import com.swgk.core.customview.WSTextView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/7/30
 */
public class ChooseFunctionPopWindow extends BasePopWindow implements View.OnClickListener {
    private ChooseFunctionPopwindowLayoutBinding mBinding;
    private ChooseTypeAdapter mAdapter;

    private List<TypeChooseEntity> mList;
    private String tittle;

    public ChooseTypeAdapter getMAdapter() {
        return mAdapter;
    }

    public void setMAdapter(ChooseTypeAdapter mAdapter) {
        this.mAdapter = mAdapter;
    }

    public ChooseFunctionPopWindow(Context context, String tittle) {
        super(context, DIR_DOWN_UP);
        this.tittle = tittle;
    }

    public ChooseFunctionPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.choose_function_popwindow_layout;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (ChooseFunctionPopwindowLayoutBinding) holder.getmBinding();
        initRecycler();
        initEvent();
    }

    public String getTittle() {
        return tittle;
    }

    public void setTittle(String tittle) {
        this.tittle = tittle;
        if (mBinding != null) {
            mBinding.tittleName.setText(TextUtils.isEmpty(tittle) ? "" : tittle);
        }
    }

    private void initRecycler() {
        mList = new ArrayList<>();
        mAdapter = new ChooseTypeAdapter(mList);
        mBinding.rvList.setLayoutManager(new GridLayoutManager(mContext, 4));
        DividerDecoration.builder(mContext)
                .color(Color.TRANSPARENT)
                .size(10, TypedValue.COMPLEX_UNIT_DIP)
                .build()
                .addTo(mBinding.rvList);
        mBinding.rvList.setAdapter(mAdapter);
    }

    private void initEvent() {
        mBinding.setOnClick(this);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            mAdapter.notifyDataSetChanged();
        });
    }

    public void setData(List<TypeChooseEntity> list) {
        mList.clear();
        if (list != null) {
            mList.addAll(list);
        }
        mAdapter.notifyDataSetChanged();
    }

    public List<TypeChooseEntity> getMList() {
        return mList;
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.resetBtn) {
            cleanSelector();
        } else if (v == mBinding.confirmBtn) {
            if (onResultClick != null) {
                List<TypeChooseEntity> entity = mAdapter.getData();
                List<TypeChooseEntity> entitySelect = new ArrayList<>();
                if (entity.size() > 0) {
                    for (TypeChooseEntity typeEntity : entity) {
                        if (typeEntity.isSelect()) {
                            entitySelect.add(typeEntity);
                        }
                    }
                }
                onResultClick.result(entitySelect, null, null);
            }
            dissMiss();
        }
    }

    /**
     * 清空选择的数据
     */
    public void cleanSelector() {
        for (TypeChooseEntity entity : mList) {
            entity.setSelect(false);
        }
        mAdapter.clearSelects();
        mAdapter.notifyDataSetChanged();
    }


    public class ChooseTypeAdapter extends BaseQuickAdapter<TypeChooseEntity, BaseViewHolder> {
        HashMap<String, TypeChooseEntity> mSelects = new HashMap<>();

        public ChooseTypeAdapter(@Nullable List<TypeChooseEntity> data) {
            super(R.layout.choose_type_pop_item, data);
        }

        public void clearSelects() {
            mSelects.clear();
        }

        @Override
        protected void convert(BaseViewHolder helper, TypeChooseEntity item) {
            WSTextView typeTv = helper.getView(R.id.type_name);
            if (item.isSelect()) {
                mSelects.put(item.getNo(), item);
                textSelect(typeTv, true);
            } else {
                textSelect(typeTv, false);
            }
            helper.setText(R.id.type_name, item.getName());

            typeTv.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onClick(View v) {
                    if (mSelects != null && mSelects.size() >= 5) {
                        if (typeTv.isSelected()) {
                            textSelect(typeTv, false);
                            item.setSelect(false);
                            mSelects.remove(item.getNo(), item);
                            return;
                        }
                        MToast.showToast(helper.itemView.getContext(), "最多可选5项");
                        return;
                    }

                    if (!item.isSelect()) {
                        textSelect(typeTv, true);
                        item.setSelect(true);
                        mSelects.put(item.getNo(), item);
                    } else {
                        textSelect(typeTv, false);
                        item.setSelect(false);
                        mSelects.remove(item.getNo(), item);
                    }
                }
            });
        }
    }

    /**
     * 切换文字选中状态
     *
     * @param typeTv
     * @param select
     */
    private static void textSelect(WSTextView typeTv, boolean select) {
        if (select) {
            typeTv.setBold(WSTextView.BOLD_MEDIUM);
            typeTv.setTextColor(Color.parseColor("#FF7F00"));
        } else {
            typeTv.setBold(WSTextView.BOLD_REGULAR);
            typeTv.setTextColor(Color.parseColor("#666666"));
        }
        typeTv.setSelected(select);

    }
}
